//SUMMARY:   This performs the replication analysis for Ballard-Rosa, Malik, Rickard & Scheve (2021)

clear all
set more off

//Set directory
cd "C:\Users\cambr\Dropbox\Conjoint Taxation\UK study\AuthoritarianDraft\Submissions\CPS\Replication materials\"

use "Data\UK full survey (indiv, with contextual data).dta", clear

* Set survey weights
svyset caseid [pweight=weight]

* Define demographic and regional controls
global DemogControls "female age higherCert universityEduc isMarried " //numChild hh_inc high_income religImport age3150 age5170 agegt70 educ
global RegionControls "pcnuk2015 chg_immig" //pcRural ratio80202016

* Define treatment and IV measures
global treatmentIPW ipw_1991_2007_weighted //ipw_1991_2015_weighted //"ipw_1991_2007_weighted" //"ipw_2000_2015_weighted"
global ipw_IV ipwUS_1991_2007_weighted //ipwUS_1991_2015_weighted //"ipwUS_1994_2007_weighted" //"ipwUS_2000_2015_weighted"
local timeLabel = "91-07"
gen ln_shock = ln($treatmentIPW)
gen ln_shockUS = ln($ipw_IV)

* Label variables used:
label var ln_shock "ln $\Delta$ IPW (`timeLabel')"
label var ln_shockUS "ln $\Delta$ IPW (US, `timeLabel')"
label var $treatmentIPW "$\Delta$ IPW (`timeLabel')"
label var $ipw_IV "$\Delta$ IPW US (`timeLabel')"
label var female "Female"
label var age "Age"
label var educ "Education"
label var isMarried "Married"
label var pcnuk2015 "% non-UK born"
label var chg_immig "$\Delta$ % non-UK born"
label var changedPC "Has moved"
label var hh_inc "HH income"
label var ratio80202016 "Inequality"
label var workingClass "Working Class"
label var middleClass "Middle Class"


//FIGURE 1:  Vote Leave by ASC
set scheme s1mono
label var votedLeave "Voted Leave"
label var asc "ASC"
lpoly votedLeave asc, kernel(epanechnikov) bwidth(.25) degree(0) noscatter title("") addplot(hist asc,lcolor(gs13) fcolor(none)) legend(lab(1 "Local polynomial smooth") lab(2 "Density") region(fcolor(white) lcolor(white)) size(small)) note("")
graph export "Output\Leave vote by ASC.png", as(png) replace

//TABLE I: OLS results
regress asc ln_shock [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/baseline", replace ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/baseline", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/baseline", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

//TABLE II: IV results
eststo clear
ivreg2 asc (ln_shock = ln_shockUS) [pweight=weight], cluster(ttwa_num)
local fstat = round(e(rkf),1)
eststo: ivreg2 asc (ln_shock = ln_shockUS) [pweight=weight], cluster(ttwa_num) first savefirst savefprefix(alpha)
outreg2 [alphaln_shock est1] using  "Output/ivresults", replace nocons ctitle(ASC) tex(frag) bdec(3) label addtext(Weak ID F stat, `fstat')
eststo clear
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls [pweight=weight], cluster(ttwa_num)
local fstat = round(e(rkf),1)
eststo: ivreg2 asc (ln_shock = ln_shockUS) $DemogControls [pweight=weight], cluster(ttwa_num) first savefirst savefprefix(alpha)
outreg2 [alphaln_shock est1] using  "Output/ivresults", append nocons ctitle(ASC) tex(frag) bdec(3) label addtext(Weak ID F stat, `fstat') sortvar(ln_shockUS ln_shock)
eststo clear
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
local fstat = round(e(rkf),1)
eststo: ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) first savefirst savefprefix(alpha)
outreg2 [alphaln_shock est1] using  "Output/ivresults", append nocons ctitle(ASC) tex(frag) bdec(3) label addtext(Weak ID F stat, `fstat') sortvar(ln_shockUS ln_shock)

//TABLE III:  ASC subcomponents
regress avgAggression ln_shock $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/subindex", replace ctitle(Aggr. OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 avgAggression (ln_shock = ln_shockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/subindex", append ctitle(Aggr. IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress avgSubmission ln_shock $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/subindex", append ctitle(Subm. OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 avgSubmission (ln_shock = ln_shockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/subindex", append ctitle(Subm. IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress avgConventionalism ln_shock $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/subindex", append ctitle(Conv. OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 avgConventionalism (ln_shock = ln_shockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/subindex", append ctitle(Conv. IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

//TABLE IV:  BSA
* See BSA replication do file

//APPENDIX
//TABLE A2: Effect by age
regress asc ln_shock female age higherCert universityEduc isMarried $RegionControls [pweight=weight], cluster(ttwa_num)
regress asc c.ln_shock##c.age female  higherCert universityEduc isMarried $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/shockXage", replace ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

margins, dydx(ln_shock)  at(age=(20(5)90)) post
est sto shockXage

//FIGURE A1:  Marginal effect of shock by age
coefplot (shockXage, at yaxis(1) yscale(alt range(-.05 .2) axis(1)) ylabel(-.05(.05).2, axis(1)) recast(line) lwidth(*2) ciopts(recast(rline) lpattern(dash))), addplot(histogram age, frac lcolor(gs13) fcolor(none) yaxis(2) yscale(alt range(0 .075) axis(2)) ylabel(0(.025).075, axis(2)) leg(off)  below) nodrop xlabel(20(10)80) ///
  ytitle(Marginal effect of shock on ASC) xtitle(Age) title("Effects of China shock by age") yline(0)
graph export "Output\China shock by age.png", as(png) replace

gen ageCohort = 1
replace ageCohort = 2 if age3150 == 1
replace ageCohort = 3 if age5170 == 1
replace ageCohort = 4 if agegt70 == 1
label def cohortLab 1 "<=30" 2 "31-50" 3 "51-70" 4 ">70"
label val ageCohort cohortLab
  
regress asc c.ln_shock##i.ageCohort female  higherCert universityEduc isMarried $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/shockXage", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

margins, dydx(ln_shock) at(ageCohort = (1 2 3 4))
marginsplot, yline(0) leg(off) title("Effects of China shock by age cohort") xtitle("Age cohort") ytitle("Marginal effect of shock on ASC") addplot(hist ageCohort, frac discrete lcolor(gs13) fcolor(none) yaxis(2) xlabel(1(1)4) yscale(alt range(0 .4) axis(2)) ylabel(0(.1).4, axis(2)) leg(off))
graph export "Output\China shock by age cohort.png", as(png) replace

//TABLE A3: Labor market consequences
* See regional results replication do file

//TABLE A4: Regional mobility
* See regional results replication do file

//FIGURE A2: Distribution of ASC
hist asc, den

//FIGURE A3: Distribution of China shock
hist ln_shock, den

//TABLE A5: Vote Leave
regress votedLeave asc [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/brexitvote", replace ctitle(Vote Leave) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress votedLeave asc $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/brexitvote", append ctitle(Vote Leave) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

//TABLE A6: Alternate covariates (OLS)
regress asc ln_shock $DemogControls $RegionControls ownIncLower ownIncUpper [pweight=weight], vce(cluster ttwa_num)
outreg2 using  "Output/appendix_covariates_ols", replace ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls $RegionControls ratio80202016 [pweight=weight], vce(cluster ttwa_num)
outreg2 using  "Output/appendix_covariates_ols", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls $RegionControls rightwing [pweight=weight], vce(cluster ttwa_num)
outreg2 using  "Output/appendix_covariates_ols", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls $RegionControls middleClass workingClass [pweight=weight], vce(cluster ttwa_num)
outreg2 using  "Output/appendix_covariates_ols", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock $DemogControls $RegionControls ownIncLower ownIncUpper ratio80202016 rightwing middleClass workingClass [pweight=weight], vce(cluster ttwa_num)
outreg2 using  "Output/appendix_covariates_ols", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

//TABLE A7: Alternate covariates (IV)
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls ownIncLower ownIncUpper [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/appendix_covariates", replace ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls ratio80202016 [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/appendix_covariates", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls rightwing [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/appendix_covariates", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls middleClass workingClass [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/appendix_covariates", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock = ln_shockUS) $DemogControls $RegionControls ownIncLower ownIncUpper ratio80202016 rightwing middleClass workingClass [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/appendix_covariates", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))

//TABLE A8: Other codings for main treatment
egen medShock = median($treatmentIPW)
gen highShock = cond($treatmentIPW >= medShock, 1, 0) if $treatmentIPW != .
egen medShockUS = median($ipw_IV)
gen highShockUS = cond($ipw_IV >= medShockUS, 1, 0) if $ipw_IV != .
label var highShock "High $\Delta$ IPW (91-07)"
label var highShockUS "High $\Delta$ IPW US (91-07)"
label var $treatmentIPW "$\Delta$ IPW (91-07)"
label var $ipw_IV "$\Delta$ IPW US (91-07)"

regress asc highShock $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/otherIPW", replace ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (highShock = highShockUS) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/otherIPW", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc $treatmentIPW $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/otherIPW", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc ($treatmentIPW = $ipw_IV) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/otherIPW", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust)) sortvar(highShock $treatmentIPW)

//TABLE A9: Alternate time periods for treatment
gen ln_shock9115 = ln(ipw_1991_2015_weighted)
gen ln_shockUS9115 = ln(ipwUS_1991_2015_weighted)
gen ln_shock0007 = ln(ipw_2000_2007_weighted)
gen ln_shockUS0007 = ln(ipwUS_2000_2007_weighted)
label var ln_shock9115 "ln $\Delta$ IPW (1991-2015)"
label var ln_shock0007 "ln $\Delta$ IPW (2000-2007)"

regress asc ln_shock9115 $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/otherperiods", replace ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock9115 = ln_shockUS9115) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/otherperiods", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock0007 $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/otherperiods", append ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock0007 = ln_shockUS0007) $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num) 
outreg2 using  "Output/otherperiods", append ctitle(ASC IV) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust)) sortvar(ln_shock9115 ln_shock0007)

//TABLE A10:  Controlling for elite opinion
regress asc ln_shock publicOfficials $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/pubOfficials", replace ctitle(ASC OLS) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
ivreg2 asc (ln_shock = ln_shockUS) publicOfficials $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
local fstat = round(e(rkf),1)
outreg2 using  "Output/pubOfficials", append nocons ctitle(ASC IV) tex(frag) bdec(3) label adds(TTWAs, e(N_clust)) addtext(Weak ID F stat, `fstat')

//TABLE A11:  Controlling for manufacturing
gen numberManufEmp1991 = totalpop1991 * percentmanufemp1991 * .01
gen log_numberManuf1991 = log(numberManufEmp1991)
label var log_numberManuf1991 "Manuf. employees (log, 1991)"
gen highManuf1991 = cond(percentmanufemp1991 >= 22.5, 1, 0) if percentmanufemp1991 != .
label var highManuf1991 "High manuf. (%, 1991)"
gen lowManuf1991 = cond(percentmanufemp1991 <= 15.1, 1, 0) if percentmanufemp1991 != .
label var lowManuf1991 "Low manuf. (%, 1991)"
gen chgManuf_1991_2015 = permanufemp2015 - percentmanufemp1991
label var chgManuf_1991_2015 "\Delta % Manuf. emp. (1991-2015)"

regress asc ln_shock log_numberManuf1991 $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/manuf_emp", replace ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock lowManuf1991 highManuf1991 $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/manuf_emp", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
regress asc ln_shock chgManuf_1991_2015 $DemogControls $RegionControls [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/manuf_emp", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust)) sortvar(ln_shock log_numberManuf1991 lowManuf1991 highManuf1991 chgManuf_1991_2015)

//TABLE A12:  Individual mobility
label var changedPC "Changed postcode"
regress asc c.ln_shock##changedPC $DemogControls $RegionControls  [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/mobility", replace ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust))
label var movedOutsideArea "Moved last 20 yrs"
regress asc c.ln_shock##movedOutsideArea $DemogControls $RegionControls  [pweight=weight], cluster(ttwa_num)
outreg2 using  "Output/mobility", append ctitle(ASC) tex(frag) nocons bdec(3) label adds(TTWAs, e(N_clust)) sortvar(ln_shock changedPC movedOutsideArea)

//TABLE A13:  Summary statistics for ASC subcomponents
summ avgAggression avgSubmission avgConventionalism

//TABLE A14:  Correlation between ASC subcomponents
corr avgAggression avgSubmission avgConventionalism

//TABLE A15:  Regional outmigration by skill type
* See regional replication do file
